package patrol.mobile.com.mobilepatrol.activity.security.emergency;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.dl7.recycler.helper.RecyclerViewHelper;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.DownloadUtil;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyOutSaveManager2ZiZhiAdapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.EnsureMade2Bean;
import patrol.mobile.com.mobilepatrol.bean.OutSaveManager2;
import patrol.mobile.com.mobilepatrol.bean.OutSaveManager2Bean;
import patrol.mobile.com.mobilepatrol.bean.PeopleInfoBean;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.ToastUtils;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack2;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

/**
 * Created by fuzhen.li on 2018/9/18.
 */
public class EmergencyOutSaveManager2Activity extends BaseActivity {

    @BindView(R.id.tv_proName)
    TextView tvProName;
    @BindView(R.id.tv_proUnit)
    TextView tvProUnit;
    @BindView(R.id.tv_orgName)
    TextView tvOrgName;
    @BindView(R.id.tv_place)
    TextView tvPlace;
    @BindView(R.id.tv_upDate)
    TextView tvUpDate;
    @BindView(R.id.tv_proDirector)
    TextView tvProDirector;
    @BindView(R.id.contact)
    TextView contact;
    @BindView(R.id.tv_quantities)
    TextView tvQuantities;
    @BindView(R.id.tv_cost)
    TextView tvCost;
    @BindView(R.id.tv_yes)
    TextView tvYes;
    @BindView(R.id.tv_have)
    TextView tvHave;
    @BindView(R.id.tv_whether)
    TextView tvWhether;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;


    private List<OutSaveManager2Bean> outSaveManager2BeanList = new ArrayList<>();
    private EmergencyOutSaveManager2ZiZhiAdapter emergencyOutSaveManager2ZiZhiAdapter;
    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_emergency_out_save_manager2;
    }

    @Override
    public void setListener() {
        emergencyOutSaveManager2ZiZhiAdapter = new EmergencyOutSaveManager2ZiZhiAdapter(this);
        RecyclerViewHelper.initRecyclerViewH(this, recyclerView, emergencyOutSaveManager2ZiZhiAdapter);
        Intent intent = getIntent();
        if (intent != null) {
            String id = intent.getStringExtra("id");
            requestData(id);
        }


        emergencyOutSaveManager2ZiZhiAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                final OutSaveManager2Bean ensureMade2Bean = outSaveManager2BeanList.get(position);
                final String url = baseUrl.substring(0, baseUrl.length() - 1) + ensureMade2Bean.getFilePath().replaceAll("\\\\", "/");
                final String path = "ensureMade";
                final String fileName = ensureMade2Bean.getFileName();
                DownloadUtil.get().download(url, Environment.getExternalStorageDirectory().getAbsolutePath(), fileName, new DownloadUtil.OnDownloadListener() {
                    @Override
                    public void onDownloadSuccess(File file) {
                        try {
                            File file2 = new File(file.getAbsolutePath());
                            Intent intent2 = new Intent("android.intent.action.VIEW");
                            intent2.addCategory("android.intent.category.DEFAULT");
                            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            Uri uri = Uri.fromFile(file2);
                            //application/msword
                            //application/vnd.ms-excel
                            if (ensureMade2Bean.getFileType().contains("doc")) {
                                intent2.setDataAndType(uri, "application/msword");
                            } else if (ensureMade2Bean.getFileType().contains("xlsx")) {
                                intent2.setDataAndType(uri, "application/vnd.ms-excel");
                            } else {
                                intent2.setDataAndType(uri, "text/plain");
                            }
                            startActivity(intent2);
                        } catch (Exception e) {
                            //没有安装第三方的软件会提示
                            ToastUtils.showToast("没有找到打开该文件的应用程序");
                        }
                    }

                    @Override
                    public void onDownloading(int progress) {

                    }

                    @Override
                    public void onDownloadFailed(Exception e) {

                    }
                });
            }
        });
    }
    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(EmergencyOutSaveManager2Activity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText(ConsTitles.EMERGENCY_OUT_SAVE_Manager_2);
    }


    private void requestData(String id) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.IEMERGENCY_OUT_SAVE_MANAGER_2);
        HashMap<String, String> map = new HashMap<>();
        map.put("id", id);
        sender.requestPost(map, new HttpCallBack2(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response, String attachList) {
                OutSaveManager2 outSaveManager2 = GsonUtil.GsonToBean(response, OutSaveManager2.class);
                tvProName.setText(outSaveManager2.getProName());
                tvProUnit.setText(outSaveManager2.getProUnit());
                tvOrgName.setText(outSaveManager2.getOrgName());
                tvPlace.setText(outSaveManager2.getPlace());
                tvUpDate.setText(outSaveManager2.getUpDate());
                tvProDirector.setText(outSaveManager2.getProDirector());
                contact.setText(outSaveManager2.getContact());
                tvQuantities.setText(outSaveManager2.getQuantities());
                tvCost.setText(outSaveManager2.getCost()+"");
                tvYes.setText(outSaveManager2.getYes());
                tvHave.setText(outSaveManager2.getHave());
                tvWhether.setText(outSaveManager2.getWhether());

                if (!TextUtils.isEmpty(attachList)){
                    outSaveManager2BeanList = GsonUtil.jsonToList(attachList, OutSaveManager2Bean.class);
                    emergencyOutSaveManager2ZiZhiAdapter.updateItems(outSaveManager2BeanList);
                }
            }
        });
    }


}
